﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Phone.Framework;
using Caliburn.Micro;
using CetWin.Phone.Entity;
using CetWin.Phone.Services;

namespace CetWin.ViewModels
{
    public enum DataOperation
    {
        None,
        Create,
        Update,
        Remove
    }

    public class SaveCategoryViewModel : Screen
    {

        private CategoryService _categoryService;
        public SaveCategoryViewModel()
        {
            _categoryService = IoC.Get<CategoryService>();
            CurrentItem=PhoneStateManager.GetAppState<Category>(GlobalConstant.SelectedCategorydField);
            if (CurrentItem == null)
            {
                CurrentItem = new Category();
                _dataOperation = DataOperation.Create;
            } else
                _dataOperation = DataOperation.Update;
            CurrentItem.PropertyChanged += CurrentItem_PropertyChanged;
        }

        void CurrentItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Name")
            {
                NotifyOfPropertyChange(() => CanSubmit);
            }
        }

        private DataOperation _dataOperation;

        protected override void OnDeactivate(bool close)
        {
            PhoneStateManager.SetAppState(GlobalConstant.SelectedCategorydField, null);
            CurrentItem.PropertyChanged -= CurrentItem_PropertyChanged;
            base.OnDeactivate(close);
        }

        private Category _currentItem;

        public Category CurrentItem
        {
            get { return _currentItem; }
            set
            {
                _currentItem = value;
                NotifyOfPropertyChange(() => CurrentItem);
            }
        }

        public bool CanSubmit
        {
            get
            {
                return !string.IsNullOrEmpty(CurrentItem.Name) && CurrentItem.Name.Length > 0;
            }
        }

        public void Submit()
        {
            switch (_dataOperation)
            {
                
                case DataOperation.Create:
                    _categoryService.AddCategory(CurrentItem);
                    break;
                case DataOperation.Update:
                    _categoryService.Update();
                    break;
                case DataOperation.Remove:
                    break;
             
            }
            IoC.Get<INavigationService>().UriFor<CategoryViewModel>().Navigate();
        }

        public void Cancel()
        {
            IoC.Get<INavigationService>().GoBack();
        }

        public bool CanDelete
        {
            get
            {
                return CurrentItem.Id > 0;
            }
        }

        public void Delete()
        {
            _categoryService.Delete(CurrentItem);
            IoC.Get<INavigationService>().UriFor<CategoryViewModel>().Navigate();
        }
    }
}
